<?php
/**
 * Created by PhpStorm.
 * User: LG
 * Date: 2017/10/10
 * Time: 14:38
 */
    //获取数据库信息
//    $mysql = require_once '../config/database_dev.php';
    $mysql = [
        'hostname'        => '192.168.1.10',
        // 数据库名
        'database'        => 'runquancrm',
        // 用户名
        'username'        => 'root',
        // 密码
        'password'        => '123456',
    ];

    //连接数据库
    $con = mysqli_connect($mysql['hostname'],$mysql['username'],$mysql['password'],$mysql['database']);

    if (!$con){
        die('Could not connect: ' . $con->connect_error);
    }
    //获取数据并执行
    $sql = "SELECT contract_id, exire_date,contract_no FROM rq_contract";
    $result = $con->query($sql);
    //数据处理
    $one_month_time = 30*24*60*60;
    $time = time();
    while($row = mysqli_fetch_assoc($result)) {
        if ($row['exire_date'] + $one_month_time <= $time){
            $updateSql = "UPDATE `runquancrm`.`rq_contract` 
                          SET `is_Expire`='1',update_time={$time} 
                          WHERE (`contract_id`={$row['contract_id']})";
            $con->query($updateSql);

            $Log = [
                'contract_id'=>$row['contract_id'],
                'do_id'=>0,
                'do_type'=>3,
                'terminal_type'=>0,
                'do_time'=>$time,
                'old_value'=>'',
                'new_value'=>'合同即将到期'
            ];
            $insertLogSql = "INSERT INTO `runquancrm`.`rq_contract_log` (`contract_id`, `do_id`, `do_type`, `terminal_type`, `do_time`, `old_value`, `new_value`) 
                              VALUES ({$Log['contract_id']}, {$Log['do_id']},
                               {$Log['do_type']}, {$Log['terminal_type']},
                                {$Log['do_time']}, {$Log['old_value']},
                                 {$Log['new_value']})";

            $con->query($insertLogSql);

        }
    }



